﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{

    public partial class TBFPmx : Form
    {
        public string gysbm = "";//供应商编码
        public int fpid = 0;
        DataTable dt2 = new DataTable();


        public TBFPmx(int id)
        {
            fpid = id;
            InitializeComponent();
            firstShow();
        }

        private void TBFPmx_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            dataGridView2.Visible = false;
            this.dataGridView2.AllowUserToAddRows = false;
            this.dataGridView2.MultiSelect = false;
            this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView2.ReadOnly = true;

            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.ReadOnly = true;

            this.dataGridView3.AllowUserToAddRows = false;
            this.dataGridView3.MultiSelect = false;
            this.dataGridView3.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView3.ReadOnly = true;
        }

        public void firstShow()
        {
            SqlConnection cn1 = new SqlConnection(SQL.conStr);
            {
                cn1.Open();
                string cmdtext3 = "select  (select  convert(datetime,fprq-2)) as 发票日期,fph as 发票号 ,gys.mc as 供应商  " +
                                " FROM YK_fp0  LEFT JOIN YK_ZDGYS gys ON  gysbm = gys.BM   " +
                                "WHERE fpid= " + fpid;
                SqlCommand com = new SqlCommand(cmdtext3, cn1);
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {

                    textBox1.Text = dr["发票号"].ToString();
                    dateTimePicker1.Text = dr["发票日期"].ToString();
                    textBox12.Text = dr["供应商"].ToString();
                }
            }
            string cmdtext1 = "select  rkdh as 入库单号,jjje as 进价金额,ljje as 零价金额 from yk_fp1 where fpid=" + fpid;
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext1, cn);
                da1.Fill(dt2);
                dataGridView3.DataSource = dt2;
            }
        }

        private void textBox12_KeyPress(object sender, KeyPressEventArgs e)//供应商press
        {
            string cmdtext4 = "select bm 编码,MC AS 名称 from yk_zdgys where dh like '" + textBox12.Text.Trim() + "%' or bm like '" + textBox12.Text.Trim() + "%' or mc like '" + textBox12.Text.Trim() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridView2.DataSource = dt;
                dataGridView2.Visible = true;
                dataGridView2.Columns[0].Width = 60;
                dataGridView2.Columns[1].Width = 150;
            }
        }


        private void dataGridView2_CellMouseDoubleClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            string cmdtext5 = "SELECT bm as 编码,MC AS 名称  from yk_zdgys where  dh = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or bm = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or mc = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' ";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com = new SqlCommand(cmdtext5, cn);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    textBox12.Text = read["名称"].ToString();
                    gysbm = read["编码"].ToString();
                }
                read.Close();
            }
            dataGridView2.Visible = false;
            button2.Enabled = true;
        }


        private void button2_Click(object sender, EventArgs e)//刷新
        {
            string dtp1 = dateTimePicker3.Value.ToShortDateString();
            string dtp2 = dateTimePicker2.Value.ToShortDateString();
            DataTable dt1 = new DataTable();
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext1 = "select  rkdh as 入库单号,(select  convert(datetime,rq-2)) as 入库日期,gys.mc as 供应商," +
                                "  lrry as 制单人  FROM YK_RKD0 R0  LEFT JOIN YK_ZDGYS gys ON R0.gysbm = gys.BM   " +
                                "  where   R0.gysbm='"+gysbm+"' and r0.rkdh not in (select rkdh from YK_FP1  where fpid="+
                                fpid + ")  AND r0.rq >=(select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2 ) AND r0.rq <=(select  convert(int,(select Convert(DateTime,'" + dtp2 + "')))+2 )   order by r0.rkdh";
                SqlDataAdapter da = new SqlDataAdapter(cmdtext1, cn);
                da.Fill(dt1);
                dataGridView1.DataSource = dt1;
                dataGridView1.Columns[2].Width = 200;
            }
        }

        private void button3_Click(object sender, EventArgs e)//"<-"
        {
            for (int index = 0; index < dataGridView3.Rows.Count ; index++)
            {
                if (System.Convert.ToDecimal(dataGridView3.Rows[index].Cells[0].Value) == System.Convert.ToDecimal(dataGridView1.SelectedRows[0].Cells["入库单号"].Value))
                { return; }

            }
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext1 = "select  rkdh as 入库单号,sum(jjje) as 进价金额,sum(ljje) as 零价金额  from yk_rkd1 where rkdh=" + dataGridView1.SelectedRows[0].Cells["入库单号"].Value.ToString() + "  group by rkdh";
                SqlDataAdapter da = new SqlDataAdapter(cmdtext1, cn);
                da.Fill(dt2);
                dataGridView3.DataSource = dt2;
            }
        }

        private void button4_Click(object sender, EventArgs e)//"->"
        {

            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext3 = "   DELETE from YK_fp1 WHERE fpid = " + fpid + "  AND rkdh = " + dataGridView3.SelectedRows[0].Cells[0].Value.ToString();
                SqlCommand com = new SqlCommand(cmdtext3, cn);
                com.ExecuteNonQuery();
            }
            dataGridView3.Rows.Remove(dataGridView3.SelectedRows[0]);
        }

        private void button1_Click(object sender, EventArgs e)//保存
        {
            int dpt_1 = 0;
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            Decimal fpje=0;
            Decimal ljje=0;
            string cmdtext6 = "select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2";
            SqlConnection cn1 = new SqlConnection(SQL.conStr);//发票日期
            {
                cn1.Open();
                SqlCommand com = new SqlCommand(cmdtext6, cn1);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    dpt_1 = (int)read[0];
                }
                read.Close();
                cn1.Close();
            }
            string cmdtext2 = "  delete from yk_fp0 where fpid=" + fpid;
            cmdtext2 = cmdtext2 + "  insert  into yk_fp0 (ykbm,fpid,fprq,fph,gysbm) values ('401'," + fpid + "," + dpt_1 + ",'" + textBox1.Text + "','" + gysbm + "')";
            cmdtext2 = cmdtext2 + "  delete from yk_fp1 where fpid="+fpid;
            for (int index = 0; index < dataGridView3.Rows.Count; index++)
            {
                cmdtext2 = cmdtext2 + "  insert into yk_fp1 (fpid,rkdh,jjje,ljje) values (" + fpid + "," + System.Convert.ToDecimal(dataGridView3.Rows[index].Cells[0].Value) + "," + System.Convert.ToDecimal(dataGridView3.Rows[index].Cells[1].Value) + "," + System.Convert.ToDecimal(dataGridView3.Rows[index].Cells[2].Value) + ")";
                fpje = fpje + System.Convert.ToDecimal(dataGridView3.Rows[index].Cells[1].Value);
                ljje = ljje + System.Convert.ToDecimal(dataGridView3.Rows[index].Cells[2].Value);
            }
            cmdtext2 = cmdtext2 + "  update yk_fp0 set fpje="+fpje+",ljje="+ljje +",rkds="+dataGridView3.Rows.Count+"  where fpid="+fpid;
            
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com = new SqlCommand(cmdtext2, cn);
                com.ExecuteNonQuery();
            }
            MessageBox.Show("保存成功");
        }
    }
}